package com.vcredit.creditcat.manager;

import java.util.*;

import com.vcredit.creditcat.model.AuthLog;
import com.vcredit.creditcat.service.AuthLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IEmergencyContactManager;
import com.vcredit.creditcat.dto.Linkman;
import com.vcredit.creditcat.dto.LinkmansSource;
import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.exception.MobileValidateException;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.Relative;
import com.vcredit.creditcat.service.RelativeService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.ParamException;
import com.vcredit.framework.util.EmojiUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class EmergencyContactManager implements IEmergencyContactManager {

	@Autowired
	private RelativeService relativeService;

	@Autowired
	private CreditCatProcessManager creditCatProcessManager;

	@Autowired
	private AuthLogService authLogService;

	@Log
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean saveEmergencyContacts(Cust cust, LinkmansSource source) throws Exception {
		List<Linkman> linkmans = source.getLinkmans();
		if(linkmans.size()<2){
			throw new ParamException("紧急联系人信息不完整！");
		}
		// 将联系人转换为List对象
		List<Relative> addrs = new ArrayList<Relative>();
		for (Linkman linkman : linkmans) {
			try {
				Relative addr = new Relative(cust.getCustId(), cust.getLastProccessId(),
						Utility.clearBlank(EmojiUtil.filterEmoji(linkman.getName())),
						Utility.formatPhoneNo(EmojiUtil.filterEmoji(linkman.getMobileNo()), false),
						linkman.getRelation(), linkman.getPriority());
				addrs.add(addr);
			} catch (Exception e) {
				continue;
			}
		}
		// 查询改手机号是否存在
		List<Relative> addrBook = relativeService.findByCustId(cust.getCustId());
		// 增量比较数据
		List<Relative> realInsertRecords = increment(addrs, addrBook);
		if (null != realInsertRecords && !realInsertRecords.isEmpty()) {
			for (Relative addressList : realInsertRecords) {
				try {
					relativeService.insert(addressList);
				} catch (Exception e) {
					continue;
				}
			}
		}
		// 客户紧急联系人认证操作
		AuthItem authItem = new AuthItem();
		authItem.setAuthCode(AuthItemEnum.CONTACTS.getCode());
		creditCatProcessManager.itemDone(authItem, cust.getLastProccessId());
		authLogService.insert(new AuthLog(cust.getLastProccessId(), cust.getCustId(),
				AuthItemEnum.CONTACTS.getCode(), "", new Date()));
		return true;
	}

	private List<Relative> increment(List<Relative> more, List<Relative> dbData) {
		if (more.isEmpty() || dbData.isEmpty()) {
			return more;
		}
		List<Relative> result = new ArrayList<Relative>();
		Set<Relative> setMore = new HashSet<Relative>();
		Set<Relative> setDbData = new HashSet<Relative>();
		Set<Relative> resultData = new HashSet<Relative>();
		CollectionUtils.addAll(setMore, more);
		CollectionUtils.addAll(setDbData, dbData);
		resultData.addAll(setMore);
		resultData.removeAll(setDbData);
		result.addAll(resultData);
		return result;
	}
}
